/*******************************************************************************
 * Copyright 2010 MoBrite (A venture of Kinara Systems Private Limited)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.mobrite.domaindroid.activity;

import com.mobrite.domaindroid.R;
import com.mobrite.domaindroid.R.id;
import com.mobrite.domaindroid.R.layout;
import com.mobrite.domaindroid.db.DomainDroidDbAdapter;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class FavoriteDomainsListActivity extends ListActivity {

	private DomainDroidDbAdapter dbHelper;
	private Cursor cursor;
	SimpleCursorAdapter notes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.domain_list);
		dbHelper = new DomainDroidDbAdapter(this);
		dbHelper.open();
		fillData();
	}
	
    private void fillData() {
        // Get all of the rows from the database and create the item list
        cursor = dbHelper.getFavouriteDomains();
        startManagingCursor(cursor);
        
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{DomainDroidDbAdapter.KEY_DOMAIN, DomainDroidDbAdapter.KEY_TIMESTAMP};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.TextView01, R.id.TextView03};
        
        // Now create a simple cursor adapter and set it to display
        notes = new SimpleCursorAdapter(this, R.layout.favorite_domain_list_item, cursor, from, to);
        setListAdapter(notes);
    }
    
    @Override
    protected void onDestroy() {
    	cursor.close();
    	dbHelper.close();
    	super.onDestroy();
    }
    
    @Override
    protected void onResume() {
    	cursor.requery();
    	super.onResume();
    }
}
